package com.yiwei.zxp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BuyerGoodDetailsV2Activity;
import com.yiwei.zxp.api.bean.GoodBean;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class ShopCartShiXiaoAdapter extends ItemAdapter<GoodBean.ContentBean.GoodDetailBean, ShopCartShiXiaoAdapter.DefaultHolder> {


    private Context context;

    public ShopCartShiXiaoAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_cart_shixiao, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        GoodBean.ContentBean.GoodDetailBean item = getItem(position);
        holder.txtTitle.setText(item.getName());
        if (item.photos != null && item.photos.size() != 0) {
            Glide.with(context).load(item.photos.get(0)).asBitmap().centerCrop().placeholder(R.color.image_default).into(holder.imgGood);
        }
        holder.imgGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BuyerGoodDetailsV2Activity.class);
                intent.putExtra("goods_id", item.goods_id);
                intent.putExtra("goods_no", item.goods_no);
                intent.putExtra("shixiao", true);
                context.startActivity(intent);
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_shixiao)
        SuperButton btnShixiao;
        @BindView(R.id.img_good)
        ImageView imgGood;
        @BindView(R.id.txt_title)
        TextView txtTitle;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public String getShiXiaoId() {
        StringBuilder builder = new StringBuilder();
        for (int j = 0; j < getmSource().size(); j++) {
            builder.append(getmSource().get(j).goods_id);
            builder.append(",");
        }

        if (builder.length() != 0) {
            return builder.substring(0, builder.length() - 1);

        } else {
            return "";
        }
    }
}
